package com.ydgg.ltar.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 验证码 作者： 张叶 创建时间：2016年12月20日 下午3:22:31
 */
@Controller
public class ResultServlet {


	@ResponseBody
	@RequestMapping(value = "resultServlet/validateCode", method = RequestMethod.POST)
	public String doPost(HttpServletRequest request,
			HttpServletResponse response, String veryCode) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String result = "0";// 验证码正确
		String validateC = (String) request.getSession().getAttribute( "validateCode");
		if (veryCode == null || "".equals(veryCode)) {
			result = "1";// 验证码为空
		} else {
			validateC = validateC.toLowerCase();
			veryCode = veryCode.toLowerCase();
			if (!validateC.equals(veryCode)) {
				result = "2";// 验证码错误
			}
		}
		return result;
	}	
	
	
	public static int validate(HttpServletRequest request,HttpServletResponse response, String veryCode) {
		response.setContentType("text/html;charset=utf-8");
		String validateC = (String) request.getSession().getAttribute("validateCode");
		if (!validateC.equals(veryCode)) {
			return 1;
		}
		return 0;
	}
	
}